import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/page/login/register/email_register.dart';
import 'package:full_display/page/login/register/phone_register.dart';
import 'package:full_display/page/login/register/register_provider.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/toast/toast_util.dart';
import 'package:full_display/unit/widgets/single_select_widget/single_select_widget.dart';
import 'package:provider/provider.dart';

class RegisterWidget extends StatelessWidget {
  const RegisterWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => RegisterProvider())],
      child: const RegisterPage(),
    );
  }
}

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  final _pageController = PageController(initialPage: 0);
  var _registerProvider = RegisterProvider();

  var current = 0;

  @override
  Widget build(BuildContext context) {
    _registerProvider = context.watch<RegisterProvider>();
    return Scaffold(
      backgroundColor: ColorPlate.background,
      resizeToAvoidBottomInset: false,
      body: Container(
          margin: EdgeInsets.only(left: 30.0.W, right: 30.0.W, top: 124.0.H),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              SingleSelectWidget(
                titleList: RegisterProvider.titleList,
                callBack: (index) {
                  setState(() {
                    current = index;
                    _pageController.jumpToPage(index);
                  });
                },
              ),
              Container(
                  margin: EdgeInsets.only(top: 31.0.H),
                  height: 268.0.H,
                  child: PageView(
                    controller: _pageController,
                    onPageChanged: (index){
                      current = index;
                    },
                    children: [
                      PhoneRegister(
                      codeStr:_registerProvider.codeStr,
                        callBack: (map) {
                          _registerProvider.updatePhoneMap(map);
                        }, registerCodeCallBack: (){
                        if (_registerProvider.codeStr=='获取验证码') {
                          _registerProvider.registerCode((str){
                            showToast(str);
                          });
                        }
                      },
                      ),
                      EmailRegister(
                        codeStr:_registerProvider.codeStr,
                        callBack: (map) {
                          _registerProvider.updateEmailMap(map);
                        },
                        registerEmail: (){
                          if (_registerProvider.codeStr=='获取验证码') {
                            _registerProvider.registerEmailCode((str){
                              showToast(str);
                            });
                          }
                        },
                      ),
                    ],
                  )),
              GestureDetector(
                onTap: () {
                  if (current == 0) {
                    _registerProvider.conformVerifyCode('',_registerProvider.phoneMap,(value){
                      showToast(value);
                    });
                  }else{
                    _registerProvider.conformVerifyCode('',_registerProvider.emailMap,(value){
                      showToast(value);
                    });
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  height: 54.0.H,
                  margin: EdgeInsets.only(top: 39.0.H),
                  width: MediaQuery.of(context).size.width,
                  decoration: BoxDecoration(
                      gradient: const LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [ColorPlate.tabbar_select, ColorPlate.tabbar_gradient]),
                      borderRadius: BorderRadius.circular(27)),
                  child: Text(
                    '完成',
                    style: TextStyle(
                      color: ColorPlate.white,
                      fontSize: 17.0.SP,
                    ),
                  ),
                ),
              ),
            ],
          )),
    );
  }
}
